package com.software.sqlite.utils;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.extra.mail.Mail;
import cn.hutool.extra.mail.MailAccount;
import com.sun.mail.util.MailSSLSocketFactory;

import java.io.File;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MailUtils {

    /**
     * 根据字符串获取邮件列表
     * @param content
     * @return
     */
    public static  List<String> getListMails(String content)
    {
        List<String> result=new ArrayList<>();
        try {
            Pattern pattern = PatternPool.EMAIL;
            Matcher matcher = pattern.matcher(content);
            // 查找所有匹配项
            while (matcher.find()) {
                result.add(matcher.group());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    public static void sendMail(String to,String from,String subject,String content,boolean html)
    {
        try {
            MailAccount mailAccount = new MailAccount();
            mailAccountConfig(mailAccount);
            Mail mail=Mail.create(mailAccount);
            mail.setTos(to);
            mail.setCcs(from);
            mail.setTitle(subject);
            mail.setContent(content);
            mail.setHtml(html);
            mail.send();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void sendMail(String to, String from, String subject, String content, boolean html, File[] file)
    {
        try {
            MailAccount mailAccount = new MailAccount();
            mailAccountConfig(mailAccount);
            Mail mail=Mail.create(mailAccount);
            mail.setTos(to);
            mail.setCcs(from);
            mail.setTitle(subject);
            mail.setContent(content);
            mail.setHtml(html);
            mail.setFiles(file);
            mail.send();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void sendBatchMail(List<String> toList, String from, String subject, String content, boolean html)
    {
        try {
            MailAccount mailAccount = new MailAccount();
            mailAccountConfig(mailAccount);
            Mail mail=Mail.create(mailAccount);
            if(toList.size()>0){
                for(String to:toList){
                    mail.setTos(to);
                    mail.setCcs(from);
                    mail.setTitle(subject);
                    mail.setContent(content);
                    mail.setHtml(html);
                    mail.send();
                    Thread.sleep(500);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void mailAccountConfig(MailAccount mailAccount) throws GeneralSecurityException {
        mailAccount.setAuth(true);
        mailAccount.setSslEnable(true);
        mailAccount.setHost("smtp.163.com");
        mailAccount.setPort(465);
        mailAccount.setFrom("cstelshipnet@163.com");
        mailAccount.setUser("cstelshipnet");
        mailAccount.setPass("AShCShQ7zmdzuBn3");
        // 解决连接smtp服务失败问题，升级了hutool版本才有这个方法
        MailSSLSocketFactory sf = new MailSSLSocketFactory("TLSv1.2");
        //信任所有host
        sf.setTrustAllHosts(true);
        //自定义设置
        mailAccount.setCustomProperty("mail.smtp.ssl.socketFactory", sf);
        mailAccount.setCustomProperty("mail.smtp.ssl.protocols", "TLSv1.2");
    }

    public static void main(String[] args) {
        String to="459059244@qq.com";
        String from="cstelshipnet@163.com";
//        String text = "459059244@qq.com 572365451@qq.com";
//        List<String> mailList=MailUtils.getListMails(text);
//        System.out.println(JSON.toJSONString(mailList));
        String subject="测试"+new Date().getTime();
        String content="测试_T"+new Date().getTime()+"</br><div><div>警告详情如下：</div><ul><li>2025-05-28 12:30:10 警告1条，级别：4,警告信息：Possible port scan detected</li></ul></div>";
        MailUtils.sendMail(to,from,subject, content,true);
    }
}
